package cn.onlineclass.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

//web.xml   servlet-   filter
    @WebFilter(filterName = "time",value = "/*")
public class ExecTimeFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("time init ");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest)servletRequest;
        //request.setCharacterEncoding(?);
        long start=System.nanoTime();//开始时间
        String path=request.getServletPath();//请求地址
        filterChain.doFilter(request,servletResponse);//放行！！！！！
        long end=System.nanoTime();
        System.out.println("执行"+path+"花费了"+(end-start)+"纳秒");
    }

    @Override
    public void destroy() {
        System.out.println("time destroy ");
    }
}
